package com.bvgol.examples.nio.groupchat;/**
 * @Classname NIOClient
 * @Description TODO
 * @Date 2021/6/21 10:51
 * @Created by GUOCHEN
 */

import lombok.SneakyThrows;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @program: my-spring-completely
 * @description:
 * @author: GUOCHEN
 * @create: 2021/06/21 10:51
 */
public class GroupChatClient {

    private final String host = "127.0.0.1";
    private final static int port = 6667;
    private Selector selector;
    private SocketChannel socketChannel;
    private String clientName;

    public GroupChatClient()  {
        try {
            this.selector = Selector.open();
            this.socketChannel = SocketChannel.open(new InetSocketAddress(host,port));
            socketChannel.configureBlocking(false);

            socketChannel.register(selector, SelectionKey.OP_READ);
            this.clientName = socketChannel.getLocalAddress().toString();

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("客户端 初始化成功");
    }

    public void sendData(String msg){

        msg = clientName + "-->"+ msg;

        try {
            socketChannel.write(ByteBuffer.wrap(msg.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void receive(){
        try {

            int select = selector.select();
            if (select>0) {
                //有可用的通道
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()){
                    SelectionKey selectionKey = iterator.next();
                    if (selectionKey.isReadable()) {
                        SocketChannel channel = (SocketChannel)selectionKey.channel();

                        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                        channel.read(byteBuffer);

                        String s = new String(byteBuffer.array());

                        System.out.println(clientName+" 收到服务器消息: "+ s);
                    }
                }
                iterator.remove();
            }
            else {
//                System.out.println("没有可用的通道");

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        GroupChatClient groupChatClient = new GroupChatClient();

        //每个三秒 读取一次服务器的数据
        new Thread(){
            @SneakyThrows
            @Override
            public void run(){
                groupChatClient.receive();
                sleep(3000);
            }
        }.start();

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            groupChatClient.sendData(s);
        }
    }
}
